package cn.t.converter.user;


import cn.t.dto.user.CreateUserRequest;
import cn.t.dto.user.CustomerFirmDTO;
import cn.t.dto.user.CustomerIndividualDTO;
import cn.t.dto.user.ImportUserRequest;
import cn.t.dto.user.ParticipantInfoDTO;
import cn.t.dto.user.RoleInfoDTO;
import cn.t.dto.user.UpdateUserRequest;
import cn.t.dto.user.UserInfoDTO;
import cn.t.dto.user.UserPassportDTO;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.enums.user.PassportTypeEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author l
 */
@Component
public class UserInfoRequestConverter {


    public CustomerIndividualDTO convertCustomerInfo(UpdateUserRequest request) {

        if (request.getIndividualId() == null || request.getIndividualId() < 1L) {
            return null;
        }
        CustomerIndividualDTO dto = convertCustomerInfo((CreateUserRequest) request);
        dto.setId(request.getIndividualId());
        dto.setCustomerId(request.getIndividualId());

        return dto;
    }

    public CustomerIndividualDTO convertCustomerInfo(CreateUserRequest request) {
        CustomerIndividualDTO dto = new CustomerIndividualDTO();
        dto.setStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        dto.setGmtCreated(new Date());

        if (StringUtils.isNotBlank(request.getIndividualName())) {
            dto.setCustomerName(request.getIndividualName());
        }

        if (StringUtils.isNotBlank(request.getPhoneNumber())) {
            dto.setPhoneNumber(request.getPhoneNumber());
        }

        return dto;
    }

    public UserInfoDTO convertUserInfo(UpdateUserRequest request) {
        UserInfoDTO dto = convertUserInfo((CreateUserRequest) request);
        dto.setId(request.getUserId());
        return dto;
    }

    public UserInfoDTO convertUserInfo(CreateUserRequest request) {
        UserInfoDTO dto = new UserInfoDTO();
        dto.setUserStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        dto.setGmtCreated(new Date());
        if (StringUtils.isNotBlank(request.getLoginPassword())) {
            dto.setLoginPassword(request.getLoginPassword());
        }
        return dto;
    }

    public Map<Integer, UserPassportDTO> convertPassport(CreateUserRequest request) {

        Map<Integer, UserPassportDTO> ret = new HashMap<>();

        if (StringUtils.isNotBlank(request.getUserName())) {
            UserPassportDTO dto = new UserPassportDTO();
            dto.setPassportType(PassportTypeEnum.USER_NAME.getCode());
            dto.setPassportValue(request.getUserName());
            dto.setPassportStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
            dto.setGmtCreated(new Date());

            ret.put(PassportTypeEnum.USER_NAME.getCode(), dto);
        }

        if (StringUtils.isNotBlank(request.getCardCode())) {
            UserPassportDTO dto = new UserPassportDTO();
            dto.setPassportType(PassportTypeEnum.CARD_CODE.getCode());
            dto.setPassportValue(request.getCardCode());
            dto.setPassportStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
            dto.setGmtCreated(new Date());

            ret.put(PassportTypeEnum.CARD_CODE.getCode(), dto);
        }

        if (StringUtils.isNotBlank(request.getEmployeeCode())) {
            UserPassportDTO dto = new UserPassportDTO();
            dto.setPassportType(PassportTypeEnum.EMP_CODE.getCode());
            dto.setPassportValue(request.getEmployeeCode());
            dto.setPassportStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
            dto.setGmtCreated(new Date());

            ret.put(PassportTypeEnum.EMP_CODE.getCode(), dto);
        }

        if (StringUtils.isNotBlank(request.getPhoneNumber())) {
            UserPassportDTO dto = new UserPassportDTO();
            dto.setPassportType(PassportTypeEnum.PHONE_NUMBER.getCode());
            dto.setPassportValue(request.getPhoneNumber());
            dto.setPassportStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
            dto.setGmtCreated(new Date());

            ret.put(PassportTypeEnum.PHONE_NUMBER.getCode(), dto);
        }

        if (ret.isEmpty()) {
            return null;
        }
        return ret;
    }

    public RoleInfoDTO convertRoleInfoDTO(CreateUserRequest request) {
        if (ObjectUtils.isEmpty(request.getRoleId())) {
            return null;
        }

        RoleInfoDTO ret = new RoleInfoDTO();
        ret.setId(request.getRoleId());

        return ret;
    }

    public ParticipantInfoDTO convertParticipantInfo(CreateUserRequest request) {
        if (request.getCostCenterId() == null || request.getCostCenterId() <= 0) {
            return null;
        }

        ParticipantInfoDTO ret = new ParticipantInfoDTO();
        ret.setCostCenterId(request.getCostCenterId());

        return ret;
    }


    public CreateUserRequest convert(ImportUserRequest importUserRequest) {

        CreateUserRequest createUserRequest = new CreateUserRequest();

        createUserRequest.setRoleId(importUserRequest.getRoleId());
        createUserRequest.setEmployeeCode(importUserRequest.getEmployeeCode());
        createUserRequest.setCardCode(importUserRequest.getCardCode());
        createUserRequest.setCostCenterId(importUserRequest.getCostCenterId());
        createUserRequest.setIndividualName(importUserRequest.getIndividualName());

        return createUserRequest;
    }

    public CustomerFirmDTO convertCustomerFirm(UpdateUserRequest updateUserRequest) {
        if (updateUserRequest != null && updateUserRequest.getFirmId() != null) {
            CustomerFirmDTO customerFirmDTO = new CustomerFirmDTO();
            customerFirmDTO.setId(updateUserRequest.getFirmId());
            return customerFirmDTO;
        }
        return null;
    }

    public CustomerFirmDTO convertCustomerFirm(ImportUserRequest excelData) {
        if (excelData != null && excelData.getFirmId() != null) {
            CustomerFirmDTO customerFirmDTO = new CustomerFirmDTO();
            customerFirmDTO.setId(excelData.getFirmId());
            return customerFirmDTO;
        }
        return null;
    }


    public  Map<String, Object> qrToParamMap(String qrCode) {
        return     Arrays.stream(qrCode.split("&")).collect(Collectors.toMap(key -> key.split("=")[0], value -> value.split("=")[1]));


}
}